package com.boil.qz.safekavass.web.controller;

import com.boil.pojo.Page2Vo;
import com.boil.pojo.ResultVo;
import com.boil.qz.safekavass.model.LearnDym;
import com.boil.qz.safekavass.model.LoginUser;
import com.boil.qz.safekavass.pojo.LearnDymVo;
import com.boil.qz.safekavass.service.LearnDymService;
import com.boil.qz.safekavass.web.request.param.AddLearnDymParam;
import com.boil.qz.safekavass.web.request.param.IdParam;
import com.boil.qz.safekavass.web.request.param.LearnDymListParam;
import com.boil.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * Created by ChenYong on 2017-04-15.
 * <br>
 * 学习动态 Controller。
 */
@Controller
@RequestMapping("/web/learn/dym")
public class LearnDymController extends OkBaseController {
    /**
     * 注入动态 Service
     */
    @Autowired
    private LearnDymService learnDymService;

    /**
     * 动态列表页面。
     *
     * @param model 模型
     * @return 动态列表页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list.html")
    public String listHtml(Model model) {
        return "/learn/dym/list";
    }

    /**
     * 添加动态页面。
     *
     * @param model 模型
     * @return 添加动态页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/add.html")
    public String addHtml(Model model) {
        return "/learn/dym/add";
    }

    /**
     * 获取动态列表 json。
     *
     * @param model  模型
     * @param param  动态列表的请求参数
     * @param errors 请求参数的校验结果
     * @return 动态列表 json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list.json")
    @ResponseBody
    public ResultVo listJson(Model model, @Valid LearnDymListParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();

        // 判断请求参数是否校验通过
        Utils.hasRequestParamError(resultVo, errors);

        // 查询分页实例
        Page2<LearnDymVo> page2 = Utils.getPage2(request, param, Constants.ID_STR);
        page2.getParams().put("learnCreatePerson", param.getLearnCreatePerson());
        page2.getParams().put("learnTitle", param.getLearnTitle());
        page2.getParams().put("learnLevel", param.getLearnLevel());
        page2.getParams().put("startDate", param.getStartDate());
        page2.getParams().put("endDate", param.getEndDate());

        // 分页查询
        learnDymService.findInPage(page2);

        // 获取 DataTables 专用分页映射实例
        Page2Vo<LearnDymVo> page2Vo = Utils.getPage2Vo(param, page2);

        resultVo.setResult(page2Vo);

        return resultVo;
    }

    /**
     * 添加动态。
     *
     * @param model  模型
     * @param param  添加动态的请求参数
     * @param errors 请求参数的校验结果
     * @return 重定向到动态列表页面--->“redirect:/learn/dym/list.html”
     */
    @RequestMapping(method = RequestMethod.POST, value = "/add.html")
    public String add(Model model, @Valid AddLearnDymParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();

        // 判断请求参数是否校验通过
        Utils.hasRequestParamError(resultVo, errors);

        // 获取登录用户
        LoginUser loginUser = getLoginUser();
        // 创建动态实例
        LearnDym learnDym = new LearnDym();
        learnDym.setLearnTitle(param.getLearnTitle());
        learnDym.setLearnContent(param.getLearnContent());
        learnDym.setLoginUser(loginUser);
        learnDym.setLearnCreatePerson(loginUser.getLoginName());
        learnDym.setLearnFilesName(param.getLearnFileNames());
        learnDym.setLearnFiles(param.getLearnFilePaths());
        learnDym.setLearnLevel(param.getLearnLevel());
        learnDym.setLearnCreateTime(DateUtils.format(new Date(), Constants.DATE_TIME_FORMAT_1));

        // 保存动态
        learnDymService.save(learnDym);

        return "redirect:/web/learn/dym/list.html";
    }

    /**
     * 获取动态详细页面。
     *
     * @param model  模型
     * @param param  ID 参数
     * @param errors 请求参数的校验结果
     * @return 动态详细页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/detail.html")
    public String detailHtml(Model model, @Valid IdParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 获取动态
        LearnDym learnDym = learnDymService.findOne(param.getId());

        // 美化内容
        if (StringUtils.isNotBlank(learnDym.getLearnContent())) {
            learnDym.setLearnContent(learnDym.getLearnContent()
                    .replaceAll(Constants.CONTENT_NEWLINE_RN_RE, Constants.CONTENT_NEWLINE_BR_SPAN)
                    .replaceAll(Constants.CONTENT_NEWLINE_N_RE, Constants.CONTENT_NEWLINE_BR_SPAN)
                    .replaceAll(Constants.CONTENT_NEWLINE_BR_SPAN_RE, Constants.CONTENT_NEWLINE_BR_SPAN)
                    .replaceAll(Constants.CONTENT_NEWLINE_SHIT_RE, Constants.EMPTY_STR));
        }

        resultVo.setResult(learnDym);

        model.addAttribute(Constants.RESULT_VO, resultVo);

        return "/learn/dym/detail";
    }
}